package com.zq.englishlearnserve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zq.englishlearnserve.entity.User;
import com.zq.englishlearnserve.service.UserService;
import com.zq.englishlearnserve.mapper.UserMapper;
import com.zq.englishlearnserve.util.NickNameGenerator;
import org.springframework.stereotype.Service;

/**
 * @author topsec
 * @description 针对表【app_user】的数据库操作Service实现
 * @createDate 2024-12-23 16:42:18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Override
    public void saveUserByWxIdIfNotExist(String wxOpenId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("wx_id", wxOpenId);
        Long l = baseMapper.selectCount(queryWrapper);
        if (l <= 0) {
            User user = new User();
            user.setNickName(NickNameGenerator.generator());
            user.setWxId(wxOpenId);
            baseMapper.insert(user);
        }
    }

    @Override
    public User getByWxId(String wxOpenId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("wx_id", wxOpenId);
        return baseMapper.selectOne(queryWrapper);
    }
}




